local json      = require "cjson.safe"
local http      = require "bw.web.http_helper"
local errcode   = require "def.errcode"

local host = "http://coding1024.com:8889"
local authorization

local M = {}
function M.api(api, param, expect_err, auth)
    local ret, resp = http.post(host..api,
        json.encode(param), {authorization = auth or authorization} or nil)
    resp = assert(json.decode(resp), resp)
    assert(ret)
    assert(resp.err == (expect_err or 0), errcode.describe(resp.err))
    return resp
end

function M.login(account, password)
    local ret = M.api("/cms/user/login", {
        account = account or "root",
        password = password or "1qaz2wsx"
    })
    authorization = assert(ret.authorization)
end

return M
